package com.example.mystoryapp2.data.network.model

import android.text.TextUtils
import com.example.mystoryapp2.contexts.AppContext
import com.example.mystoryapp2.data.network.NetCallBack
import com.example.mystoryapp2.data.network.ServiceCreator
import com.example.mystoryapp2.data.network.service.UserInfoService
import com.example.mystoryapp2.domain.UserInfo
import retrofit2.Call
import retrofit2.Response

object UserModel {

    private var userService = ServiceCreator.create<UserInfoService>()
    fun loadUserinfo(callBack: ICallBack) {
        if (!TextUtils.isEmpty(AppContext.jwt)) {
            userService.getUserinfo(AppContext.jwt).enqueue(object : NetCallBack<UserInfo>() {
                override fun onResponse(call: Call<UserInfo>, response: Response<UserInfo>) {
                    val body = response.body()
                    if (body != null) {
                        AppContext.userInfo = body
                        callBack.onLoaded()
                    }else{
                        callBack.onFailed()
                    }
                }

                override fun onFailure(call: Call<UserInfo>, t: Throwable) {
                    callBack.onFailed()
                }
            })
        }
    }

    interface ICallBack{
        fun onLoaded()
        fun onFailed()
    }
}